#描述:
#--interface 选项必须传递
#--publish_port 选项可以省略
#--ret_port 选项可以省略
#--file_roots 选项可以省略
#--pillar_roots 选项可以省略
#--pillar_opts 选项可以省略


source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='

key    ,argument    ,isempty    ,desc    

--interface    ,interface    ,0    ,工作在哪个接口

--publish_port    ,publish_port    ,1    ,发布消息; master: firewall-cmd --add-port 4505/tcp

--ret_port    ,ret_port    ,1    ,与minion通信 salt指定此端口返回数据; master: firewall-cmd --add-port 4506/tcp

--file_roots    ,file_roots    ,1    ,基本环境目录

--pillar_roots    ,pillar_roots    ,1    ,基本pillar目录

--pillar_opts    ,pillar_opts    ,1    ,启动pillar

'

parse_cmd $@

echo $interface

: ${publish_port:=4505}
: ${ret_port:=4506}
: ${file_roots:=/data/minion}
: ${pillar_roots:=/data/pillar}
: ${pillar_opts:=True}


echo "$publish_port $ret_port $file_roots $pillar_roots $pillar_opts "


#https://docs.saltproject.io/salt/install-guide/en/latest/topics/install-by-operating-system/ubuntu.html
sudo curl -fsSL -o /etc/apt/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/salt/py3/ubuntu/22.04/amd64/SALT-PROJECT-GPG-PUBKEY-2023.gpg
echo "deb [signed-by=/etc/apt/keyrings/salt-archive-keyring.gpg arch=amd64] https://repo.saltproject.io/salt/py3/ubuntu/22.04/amd64/latest jammy main" | sudo tee /etc/apt/sources.list.d/salt.list
apt-get update
sudo apt-get install salt-master -y
sudo apt-get install salt-minion -y
sudo apt-get install salt-ssh -y
sudo apt-get install salt-syndic -y
sudo apt-get install salt-cloud -y
sudo apt-get install salt-api -y





cat > /etc/salt/master.d/default.conf <<EOF
interface: $interface
publish_port: $publish_port
ret_port: $ret_port
file_recv: True # minio push to master
state_top: top.sls # 高级状态
file_roots:
  base:
  - $file_roots
pillar_roots:
  base:
  - $pillar_roots
pillar_opts: $pillar_opts
#nodegroups:
#  k8s: "L@192.168.0.78,192.168.0.79,192.168.0.80,192.168.0.81,192.168.0.82" # 列表
EOF
systemctl restart salt-master.service

sudo systemctl enable salt-master && sudo systemctl start salt-master
sudo systemctl enable salt-minion && sudo systemctl start salt-minion
sudo systemctl enable salt-syndic && sudo systemctl start salt-syndic
sudo systemctl enable salt-api && sudo systemctl start salt-api
